<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\exception\ValidateException;

use app\common\model\{WorkerBank, WorkerBankToday, WorkerCate};
use app\common\validate\{WorkerValidate};

class WorkerController extends BaseController
{
    /** 新增人工 */
    public function addWorker()
    {
        $data = input('');

        try{
            validate(WorkerValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workenBankModel = new WorkerBank();
        $workenBankTodayModel = new WorkerBankToday();

        /** 添加到人工库 */
        $addFlag = $workenBankModel->add($data);

        if($addFlag['code'] != 'ok'){
            $this->error_back($addFlag['msg']);
        }

        $todayAddFlag = [
            'user_id' => $data['user_id'],
            'worker_bank_id' => $addFlag['data'],
        ];

        /** 添加到今日人工 */
        $workenBankTodayModel->add($todayAddFlag);

        $this->success_back('新增成功');
    }

    /** 今日人工列表 */
    public function getWorkerTodayList()
    {
        $data = input('');

        try{
            validate(WorkerValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workenBankTodayModel = new WorkerBankToday();

        $where[] = ['worker_today.user_id' , '=', $data['user_id']];

        $list = $workenBankTodayModel->getWorkerBandTodayList($where, $data['page'], $data['limit']);

        if(empty($list)){
            $this->error_back('暂无数据');
        }

        $this->success_back('ok', $list);
    }

    /** 修改工费 */
    public function setPrice()
    {
        $data = input('');

        try{
            validate(WorkerValidate::class)->scene('set_price')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workenBankTodayModel = new WorkerBankToday();

        $where[] = ['id' , '=', $data['worker_today_id']];

        $flag = $workenBankTodayModel->edit(['id' => $data['worker_today_id'], 'price' => $data['price']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('修改成功');
    }

    /** 人工库列表 */
    public function getWorkerList()
    {
        $data = input('');

        try{
            validate(WorkerValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workenBankModel = new WorkerBank();

        $where[] = ['user_id' , '=', $data['user_id']];

        $field = 'id, face_img, name, mobile, card_id';

        $list = $workenBankModel->getList($where, 1, $data['page'], $data['limit'], $field, 'create_time DESC');

        if($list['code'] !== 'ok'){
            $this->error_back($list['msg']);
        }

        $this->success_back('ok', $list['data']);
    }

    /** 从人工库提取到今日人工 */
    public function workerToToday()
    {
        $data = input('');

        try{
            validate(WorkerValidate::class)->scene('to_today')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workenBankTodayModel = new WorkerBankToday();

        $worker_ids = explode(',', $data['worker_ids']);

        $addArr = [];
        foreach($worker_ids as $v){
            $addInfo = [
                'user_id'        => $data['user_id'],
                'worker_bank_id' => $v
            ];

            $addArr[] = $addInfo;
        }

        $addFlag = $workenBankTodayModel->addAll($addArr);

        if($addFlag['code'] != 'ok'){
            $this->error_back($addFlag['msg']);
        }

        $this->success_back('提取成功');
    }

    /** 昨日人工列表 */
    public function getWorkerYesterdayList()
    {
        $data = input('');

        try{
            validate(WorkerValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workenBankModel = new WorkerBank();

        $where[] = ['user_id' , '=', $data['user_id']];

        $field = 'id, face_img, name, mobile, card_id';

        $list = $workenBankModel->getYesterdayList($where, $data['page'], $data['limit'], $field, 'create_time DESC');

        if($list->isEmpty()){
            $this->error_back('暂无数据');
        }

        $this->success_back('ok', $list);
    }

    /** 人工分类列表 */
    public function getWorkerCateList()
    {
        $workenCateModel = new WorkerCate();

        $list = $workenCateModel->getList([], 0);

        if($list['code'] !== 'ok'){
            $this->error_back($list['msg']);
        }

        $this->success_back('ok', $list['data']);
    }

    /** 人工库检索列表 */
    public function getWorkerSearchList()
    {
        $data = input('');

        try{
            validate(WorkerValidate::class)->scene('list_search')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $workenBankModel = new WorkerBank();

        $where[] = ['user_id' , '=', $data['user_id']];
        $where[] = ['cate_id' , '=', $data['cate_id']];

        $field = 'id, face_img, name, sex, mobile, card_id';

        $list = $workenBankModel->getList($where, 1, $data['page'], $data['limit'], $field, 'create_time DESC');

        if($list['code'] !== 'ok'){
            $this->error_back($list['msg']);
        }

        foreach($list['data'] as &$v){
            $v['age'] = getAgeByID($v['card_id']) . '岁';
            unset($v['card_id']);
        }

        $this->success_back('ok', $list['data']);
    }
}